<template>
  <div class="menu-container">
    <el-card class="box-card">
       <el-row style="margin: 10px">
        <el-col :span="24">
          <el-button type="primary" size="small" icon="el-icon-plus" 
          @click="addClick"
            >添加</el-button
          >
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="deleteClick"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="menuList" 
      style="width: 100%"    
      border
      default-expand-all 
      row-key="id"
      :tree-props="{children: 'children' }"
       @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="45"> </el-table-column>
        <el-table-column label="展开" width='100' align="center"> </el-table-column>
        <el-table-column label="图标" width="60" align="center">
          <template v-slot="slot">
            <i :class="slot.row.menu_icon"></i>
          </template>
        </el-table-column>
        <el-table-column prop="menu_name" label="菜单名称" width="100" align="center"> </el-table-column>
        <el-table-column prop="menu_url" label="路径" width="80" align="center"></el-table-column>
        <el-table-column  label="类型" width="80" align="center">
          <template v-slot="slot">
             <el-tag v-if="slot.row.menu_type<=1">菜单</el-tag>
              <el-tag v-if="slot.row.menu_type>1" type="success">按钮</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="menu_sort" label="排序" width="80" align="center"></el-table-column>
        <el-table-column
          label="菜单等级"
          width="80"
          align="center"
        >
        <template v-slot="slot">
             <el-tag  v-if="slot.row.parentId<=0">一级菜单</el-tag>
          </template>
        </el-table-column>

        
        <el-table-column label="展示" width="80" align="center">
          <template v-slot="slot">
            {{slot.row.status}}
             <el-switch v-model="slot.row.status" @change="disable(slot.row)"></el-switch>
          </template>
        </el-table-column>
        
        <el-table-column prop="address" label="操作" width="180" align="center">
          <template v-slot="slot">
            <el-button type="primary" icon="el-icon-edit" size="small"
            @click="editClick(slot.row)"
              >编辑</el-button
            >
            <el-button type="danger" icon="el-icon-delete" size="small"
             @click="deleteClick(slot.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
      background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加抽屉 -->
        <el-drawer
        title="添加菜单"
        :visible.sync="adddrawer"
        :direction="direction"
        :before-close="handleClose"
        close-on-press-escape
        @close="close"
      >
        <el-form
          :label-position="labelPosition"
          label-width="40px"
          :model="addMenuform"
          size="small"
        >
          <el-form-item label="名称">
            <el-input v-model="addMenuform.menu_name"></el-input>
          </el-form-item>
          <el-form-item label="路径">
             <el-input v-model="addMenuform.menu_url"></el-input>
          </el-form-item>
          <el-form-item label="类型">
             <el-select v-model="addMenuform.menu_type" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <!-- <el-input v-model="addMenuform.menu_type"></el-input> -->
          </el-form-item>
           <el-row :gutter="20">
            <el-col :span="12">
             <el-form-item label="图标">
            <el-input v-model="addMenuform.menu_icon"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
            <el-form-item label="上级">
              <el-input v-model="addMenuform.parentId"></el-input>
          </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="排序">
            <el-input v-model="addMenuform.menu_sort"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
            <el-form-item label="展示">
            <el-switch v-model="addMenuform.status"> </el-switch>
          </el-form-item>
            </el-col>
          </el-row>
          
          <el-form-item>
           <el-button size="small" type="primary" @click="addMenu">添加</el-button>
             <el-button size="small" type="primary" @click="close">重置</el-button>
          </el-form-item>
            
        </el-form>
      </el-drawer>
       <!-- 编辑抽屉 -->
        <el-drawer
        title="编辑菜单"
        :visible.sync="editdrawer"
        :direction="direction"
        :before-close="handleClose"
        close-on-press-escape
        @close="close"
      >
        <el-form
          :label-position="labelPosition"
          label-width="40px"
          :model="editMenuform"
          size="small"
        >
          <el-form-item label="名称">
            <el-input v-model="editMenuform.menu_name"></el-input>
          </el-form-item>
          <el-form-item label="路径">
             <el-input v-model="editMenuform.menu_url"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <!-- <el-input v-model="editMenuform.menu_type"></el-input> -->
            <el-select v-model="editMenuform.menu_type" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
           <el-row :gutter="20">
            <el-col :span="12">
             <el-form-item label="图标">
            <el-input v-model="editMenuform.menu_icon"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
            <el-form-item label="上级">
              <el-input v-model="editMenuform.parentId"></el-input>
          </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="排序">
            <el-input v-model="editMenuform.menu_sort"></el-input>
          </el-form-item>
            </el-col>
           <el-col :span="12">
            <el-form-item label="展示">
            <el-switch v-model="editMenuform.status"> </el-switch>
          </el-form-item>
            </el-col>
          </el-row>
          
          <el-form-item>
           <el-button size="small" type="primary" @click="editMenu">修改</el-button>
             <el-button size="small" type="primary" @click="close">重置</el-button>
          </el-form-item>
            
        </el-form>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import { getMenuList,deleteMenu,editMenu,addMenu,getMenuId } from "@/api/menu";
import {toTree,transListToTreeData} from '@/utils'
export default {
  name:'Menu',
  data() {
    return {
      menuList: [],// 菜单表格数据
       queryInfo: {
        query: '', // 查询参数
        pageNum: 1,// 分页页码
        pageSize: 10// 页面数据条数
      },
      total: 0,
      selectRows: [],// 全选数据
      adddrawer:false,
      editdrawer: false,
      direction: "rtl",
      labelPosition: "left",//改变表单域标签的位置
      addMenuform:{
        menu_icon:'',
        menu_name:'',
        menu_url:'',
        menu_type:'',
        menu_sort:'',
        parentId:'',
        status:false
      },
      editMenuform:{},
      options: [{
          value: '1',
          label: '菜单'
        }, {
          value: '2',
          label: '按钮'
        }]
    };
  },
  created() {
    this.getMenuList();
  },
  methods: {
    // 确认编辑
    async editMenu(){
      const id = this.editMenuform.id
      const body = this.editMenuform
      await editMenu(id, body);
      this.getMenuList();
      this.editdrawer = !this.editdrawer
      this.$message.success('修改成功')
    },
    // 点击编辑用户按钮弹出抽屉
   async editClick(row){
      const res = await getMenuId(row.id)
      this.editMenuform = res
       this.editdrawer = !this.editdrawer;
    },
    // 确认添加按钮
    async addMenu(){
      const body = this.addMenuform
     const res =  await addMenu(this.addMenuform)
      this.getMenuList();
      this.adddrawer = !this.adddrawer;
      this.$message.success('添加成功')
    },
    // 点击添加按钮弹出抽屉
    addClick(){
      this.adddrawer = !this.adddrawer;
    },
    // 关闭抽屉提示
    handleClose(done) {
      done();
      // this.$confirm("确认关闭？")
      //   .then((_) => {
      //     done();
      //   })
      //   .catch((_) => {});
    },
    // 关闭抽屉就清空
    close(){
      this.editMenuform = {}
      this.addMenuform={}
    },
    // 滑块功能
    async disable(row) {
      const { id, status } = row;
      const res = await editMenu(id, { status: status });
      this.$message.success(`${status ? "已展示" : "取消展示"}`);
    },
    // 文章表格数据
    async getMenuList() {
      const { list, pageNum, pageSize, total } = await getMenuList(this.queryInfo);
     this.queryInfo.pageNum =Number(pageNum);
      this.queryInfo.pageSize =Number(pageSize);
      this.total = total;
      //转为tree树型格式
      // this.menuList=toTree(list)
      this.menuList=list
    },
    // 设置页面数据条数
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.getMenuList();
    },
    // 设置分页页码
    handleCurrentChange(val) {
      this.queryInfo.pageNum=val
      this.getMenuList();
    },
    // 删除用户
    deleteClick(row){
      if (row.id) {
        this.$confirm("你确定要删除选中项吗", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await deleteMenu({ ids: [row.id] });
            this.getMenuList();
            this.$message.success("删除成功");
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      } else {
        // 批量删除
        if (!this.selectRows.length)
          return this.$message.warning("请选择要删除的选中项");
        const ids = this.selectRows.map((item) => item.id);
        this.$confirm("你确定要删除选中项吗", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            await deleteMenu({ ids });
            this.getMenuList();
            this.$message.success("删除成功");
          })
          .catch(() => {
            this.$message.info("已取消删除");
          });
      }
    },
    // 全选数据
    handleSelectionChange(val) {
      this.selectRows=val
      }
  },
};
</script>

<style lang="scss">
.box-card {
  margin: 20px;
}
.el-table{
  margin-bottom: 20px;
}
.el-image{
  border-radius: 5px;
}
.el-form{
  margin: 40px 8px;
}
</style>
